package qmlt.dataset.filter;

import java.util.HashMap;
import java.util.Map;

import qmlt.dataset.Attribute;
import qmlt.dataset.DataSet;
import qmlt.dataset.DataSetBase;
import qmlt.dataset.DataSetDef;
import qmlt.dataset.Instance;

public class CategoricalOutputToNumericFilter implements Filter
{

	public final Map<String, Float>	conversionMap;

	public CategoricalOutputToNumericFilter(Map<String, Float> conversionMap)
	{
		this.conversionMap = conversionMap;
	}

	public CategoricalOutputToNumericFilter(String posString, Float posValue, String negString,
			Float negValue)
	{
		conversionMap = new HashMap<String, Float>();
		conversionMap.put(posString, posValue);
		conversionMap.put(negString, negValue);
	}

	@Override
	public DataSet filter(DataSet ds)
	{
		assert ds.getTargetDef().type.equals(Attribute.STRING) : "this filter applies to categorical output only.";

		Attribute oa = new Attribute(Attribute.FLOAT, ds.getTargetDef().noValueString);
		DataSetDef newDef = new DataSetDef(ds.getId() + "-cotn", ds.getFeatureDefs(), oa);
		DataSet newDs = new DataSetBase(newDef, ds);

		for (Instance inst : ds.getInstances())
		{
			String target = (String) inst.getTarget();
			Instance newInst = inst.shallowClone(newDs);
			newInst.setTarget(conversionMap.get(target));
		}

		return newDs;
	}

}
